package com.estate.asset.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.estate.asset.feign.QuartersFeign;
import com.estate.asset.service.AssetService;
import com.estate.bean.Device;
import com.estate.bean.Parking;
import com.estate.mapper.DeviceMapper;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AssetServiceImpl extends ServiceImpl<DeviceMapper, Device> implements AssetService {

    @Resource
    private QuartersFeign quartersFeign;
    @Override
    public JsonResult listByPage(int page, int size) {
        PageHelper.startPage(page, size);
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        List<Device> list = list(wrapper);
        list.forEach(e->{
            e.setQuarters(quartersFeign.find(e.getCommunityId()));
        });
        PageInfo<Device> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }

    @Override
    public JsonResult saveDevice(Device device) {
        device.setIsDelete(0);
        log.info("新增device数据:{}", device);
        boolean b = save(device);
        if (!b) {
            return ResultTool.fail(501, "新增车位失败");
        }
        return ResultTool.success("新增成功");
    }

    @Override
    public JsonResult updateDevice(Device device) {
        log.info("修改device数据:{}", device);
        boolean b = updateById(device);
        if (!b) {
            return ResultTool.fail(503, "修改车位失败");
        }
        return ResultTool.success("修改成功");
    }

    @Override
    public JsonResult deleteDevice(String deviceId) {
        log.info("删除deviceId数据:{}", deviceId);
        boolean b = removeById(deviceId);
        if (!b) {
            return ResultTool.fail(502, "删除车位失败");
        }
        return ResultTool.success("删除成功");
    }
}
